//
//  CommentUtils.m
//  JiaoX
//
//  Created by yinqy on 15/1/9.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "CommentUtils.h"
#import "UserDefaultsHelper.h"
#import "JXLoginController.h"
#import "JXNavigationController.h"
#import "HomeDataTool.h"
#import "CommentViewFrame.h"


@implementation CommentUtils
+ (void)replyCommentWithController:(UIViewController *)controller replyUrl:(NSString *)replyUrl ReplyType:(NSString *)replyType replyMainId:(NSString *)replyMainId commentId:(NSString *)commentId replyContent:(NSString *)replyContent userIdOther:(NSString *)userIdOther commentType:(NSString *)commentType success:(void (^)(CommentInfo *result))success failure:(void (^)(NSError *error))failure{

    [HomeDataTool replyCommentWithReplyUrl:replyUrl ReplyType:replyType replyMainId:replyMainId commentId:commentId replyContent:replyContent userIdOther:userIdOther commentType:commentType success:success failure:failure];
}

+ (void)replyResultHandle:(NSMutableArray *)comtFrameArray replyType:(NSString *)replyType commentType:(NSString *)commentType currentComtId:(NSString *)currentComtId userIdOther:(NSString *)userIdOther userNameOther:(NSString *)userNameOther replyContent:(NSString *)replyContent section:(NSInteger)section orderBy:(NSString *)orderBy doSomeBefore:(void (^)())doSomeBefore doSomeAfter:(void (^)())doSomeAfter{
    if (doSomeBefore) {
        doSomeBefore();
    }
    
    NSString *headImage = [UserDefaultsHelper stringForKey:KEY_USER_IMG];
    NSString *commentUserId = [UserDefaultsHelper stringForKey:KEY_USER_ID];
    NSString *commentName = [UserDefaultsHelper stringForKey:KEY_NICK_NAME];
    
    CommentInfo *info = [[CommentInfo alloc]init];
    [info setHeadImage:headImage];
    [info setCommentUserId:commentUserId];
    [info setCommentId:[NSString stringWithFormat:@"%@",currentComtId]];
    [info setCommentName:commentName];
    [info setCommentContent:replyContent];
    [info setCommentTime:@"刚刚"];
    [info setUserIdOther:userIdOther];
    [info setUserNameOther:userNameOther];
    [info setIsShowAll:1];
    [info setCommentGender:[UserDefaultsHelper stringForKey:KEY_USER_GENDER]];
    [info setCommentLevel:(int)[UserDefaultsHelper integerForKey:KEY_USER_LEVEL]];
    [info setReComment:[NSMutableArray array]];
    
    if([@"1" isEqualToString:commentType] | [@"2" isEqualToString:commentType]){
        //回复主评论 或者 //回复子评论
        
        CommentViewFrame *frame = comtFrameArray[section];
        
        CommentInfo *mainInfo = frame.comtInfo;
        
        if (mainInfo.reComment.count < COMT_PAGE_CHILD_NUM) {
            if ([replyType isEqualToString:REPLY_TYPE_POSTSID]) {//如果是帖子。子评论添加到后面。
                [mainInfo.reComment addObject:info];
            }else{
                if (mainInfo.reComment.count) {
                    [mainInfo.reComment insertObject:info atIndex:0];
                }else{
                    [mainInfo.reComment addObject:info];
                }
            }
        }
        
        //先移除旧的
        [comtFrameArray removeObjectAtIndex:section];
        //再添加新的
        CommentViewFrame *newFrame = [[CommentViewFrame alloc]init];
        [newFrame setComtInfo:mainInfo];
        [comtFrameArray insertObject:newFrame atIndex:section];
        
    }else{
        //回复帖子(评测，商品)
        CommentViewFrame *newFrame = [[CommentViewFrame alloc]init];
        [newFrame setComtInfo:info];
        
        if ([replyType isEqualToString:REPLY_TYPE_POSTSID] && ![orderBy isEqualToString:DESC]) {
            //回复的是帖子,且已经显示回复帖子的评论条数大于50时，回复内容不添加
            if (comtFrameArray.count < COMT_PAGE_NUM) {
                [comtFrameArray addObject:newFrame];
            }
        } else {//评测 or 商品 or 帖子倒序查看的时候
            if(!comtFrameArray.count){
                [comtFrameArray addObject:newFrame];
            }else{
                [comtFrameArray insertObject:newFrame atIndex:0];
            }
        }
    }
    
    if (doSomeAfter) {
        doSomeAfter();
    }
    
}
@end
